package com.googlecode.stockhamster.client.console;

import java.net.MalformedURLException;
import java.util.Random;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.rumor.IRumor;
import com.googlecode.stockhamster.stockstat.IStockStat;
import com.googlecode.stockhamster.utils.XFireStub;

public class RumorDispatcher {

	private static final Double DEFAULT_OFFSET = 0.05;

	private IRumor rumor;
	private IStockStat status;
	private Integer stocks[];

	public RumorDispatcher() throws MalformedURLException {
		rumor = new XFireStub<IRumor>(Constant.rumorUrl, IRumor.class)
				.getStub();
		status = new XFireStub<IStockStat>(Constant.stockStatUrl,
				IStockStat.class).getStub();
		stocks = status.getStocks();
	}
	
	public void dispatch(Double offset) {
		if (offset == null)
			offset = DEFAULT_OFFSET;
		Random r = new Random();
		for (Integer id : stocks) {
			String name = status.getName(id);
			Boolean ret = rumor.makeRumorCompany(Constant.rumorAdminCode, name,
					r.nextDouble() / 100 - 0.005 + offset, r.nextDouble() / 100
							- 0.005 + offset, r.nextDouble() / 100 - 0.005
							+ offset, r.nextDouble() / 100 - 0.005 + offset);
			System.out.println("Dispatch rumor on " + name + ": " + ret);
		}
	}

	/**
	 * @param args
	 * @throws MalformedURLException
	 */
	public static void main(String[] args) throws MalformedURLException {
		new RumorDispatcher().dispatch(null);
	}
}
